package com.kele.jucutils;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author kele
 * @date 2021/10/11 15:31
 * @description CyclicBarrier 加法计数器
 */
public class CyclicBarrierTest {
    public static void main(String[] args) {
        Runnable barrierAction;
        CyclicBarrier cyclicBarrier=new CyclicBarrier(5, ()->{
            System.out.println("上了五天班了");
        });
        for (int i = 1; i <=5 ; i++) {
            final int temp=i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"上了"+temp+"天班");
                try {
                    /**
                     * 等待数量到多少才会继续执行
                     */
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
